package com.atguigu01.collection;

import com.atguigu01.collection.CirclePointsDemo8;

import java.awt.*;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class CirclePointsDemo9 extends JPanel {
    private static final int RADIUS = 300; // 设置圆的半径
    private static final int DIVISIONS = 12; // 设置等分数量
    private static final int[] CIRCLE_RADII = {60, 120, 180, 240}; // 设置4个同心圆的半径
    private static final int POINTS_PER_DIVISION = 83; // 设置每个扇形中的点数
    private static final int POINT_RADIUS_DIFF = 10; // 设置同心圆之间的半径差值
    private static final int[] POINT_CIRCLE_RADII = {60, 120, 180, 240}; // 点的坐标所属的同心圆半径

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g; // 将Graphics对象转换为Graphics2D对象
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); // 开启抗锯齿

        int centerX = getWidth() / 2; // 获取面板中心横坐标
        int centerY = getHeight() / 2; // 获取面板中心纵坐标
        System.out.println("圆心坐标: (" + centerX + ", " + centerY + ")\n"); // 输出圆心坐标

        // 绘制坐标轴
        g2d.setColor(Color.BLUE); // 设置坐标轴颜色为蓝色
        g2d.setStroke(new BasicStroke(2)); // 设置线宽为2
        g2d.drawLine(centerX, 0, centerX, getHeight()); // 绘制y轴
        g2d.drawLine(0, centerY, getWidth(), centerY); // 绘制x轴

        // 绘制坐标轴刻度
        g2d.setStroke(new BasicStroke(1)); // 设置线宽为1
        int interval = RADIUS / 12; // 刻度间距
        for (int i = -12; i <= 12; i++) {
            if (i != 0) { // 不绘制原点处的刻度
                // 绘制y轴刻度
                int y = centerY - i * interval;
                g2d.drawLine(centerX - 5, y, centerX + 5, y);
                g2d.drawString(String.valueOf(i * interval), centerX + 10, y + 5);
                // 绘制x轴刻度
                int x = centerX + i * interval;
                g2d.drawLine(x, centerY - 5, x, centerY + 5);
                g2d.drawString(String.valueOf(i * interval), x - 5, centerY + 20);
            }
        }

        // 绘制圆和同心圆中的点
        g2d.setColor(Color.BLACK); // 设置绘图颜色为黑色
        // 绘制圆
        g2d.drawOval(centerX - RADIUS, centerY - RADIUS, RADIUS * 2, RADIUS * 2);

        // 绘制4个同心圆及其中的点
        for (int i = 0; i < CIRCLE_RADII.length; i++) {
            int circleRadius = CIRCLE_RADII[i]; // 获取当前同心圆的半径
            int pointRadiusDiff = POINT_RADIUS_DIFF * i; // 计算当前同心圆与上一个同心圆的半径差值
            int pointRadius = circleRadius - pointRadiusDiff; // 计算当前同心圆中点的半径
            int pointCount = POINTS_PER_DIVISION * (i + 1); // 计算当前同心圆中点的总数

            // 绘制同心圆
            g2d.drawOval(centerX - circleRadius, centerY - circleRadius, circleRadius * 2, circleRadius * 2);

            // 绘制同心圆中的点
            for (int j = 0; j < pointCount; j++) {
                double angle = 2 * Math.PI * j / pointCount; // 计算当前点的角度
                int x = centerX + (int) (pointRadius * Math.cos(angle)); // 计算当前点的横坐标
                int y = centerY + (int) (pointRadius * Math.sin(angle)); // 计算当前点的纵坐标
                System.out.println("点(" + j + ")坐标: (" + x + ", " + y + ")"); // 输出点的坐标
                g2d.fillOval(x, y, 2, 2); // 绘制当前点
            }
        }
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("Circle Divide");
        frame.setSize(800, 800);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        CirclePointsDemo9 panel = new CirclePointsDemo9();
        frame.add(panel);
        frame.setVisible(true);
    }
}